package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ContractTemplate;
import com.ruoyi.system.domain.ContractTemplateType;
import com.ruoyi.system.mapper.ContractTemplateMapper;
import com.ruoyi.system.mapper.ContractTemplateTypeMapper;
import com.ruoyi.system.service.IContractTemplateService;

/**
 * u5408u540cu6a21u677fu7ba1u7406 u670du52a1u5b9eu73b0
 *
 * @author ruoyi
 */
@Service
public class ContractTemplateServiceImpl implements IContractTemplateService
{
    @Autowired
    private ContractTemplateMapper contractTemplateMapper;

    @Autowired
    private ContractTemplateTypeMapper contractTemplateTypeMapper;

    /**
     * u67e5u8be2u5408u540cu6a21u677fu5217u8868
     *
     * @param contractTemplate u5408u540cu6a21u677fu4fe1u606f
     * @return u5408u540cu6a21u677fu96c6u5408
     */
    @Override
    public List<ContractTemplate> selectContractTemplateList(ContractTemplate contractTemplate)
    {
        List<ContractTemplate> templateList = contractTemplateMapper.selectContractTemplateList(contractTemplate);
        for (ContractTemplate template : templateList)
        {
            // u67e5u8be2u5173u8054u7684u7c7bu578bID
            List<Long> typeIds = contractTemplateTypeMapper.selectTypeIdsByTemplateId(template.getTemplateId());
            template.setTypeIds(typeIds.toArray(new Long[0]));
        }
        return templateList;
    }

    /**
     * u6839u636eu7c7bu578bIDu67e5u8be2u5173u8054u7684u5408u540cu6a21u677fu5217u8868
     *
     * @param typeId u5408u540cu7c7bu578bID
     * @return u5408u540cu6a21u677fu96c6u5408
     */
    @Override
    public List<ContractTemplate> selectContractTemplatesByTypeId(Long typeId)
    {
        List<ContractTemplate> templateList = contractTemplateMapper.selectContractTemplatesByTypeId(typeId);
        for (ContractTemplate template : templateList)
        {
            // u67e5u8be2u5173u8054u7684u7c7bu578bID
            List<Long> typeIds = contractTemplateTypeMapper.selectTypeIdsByTemplateId(template.getTemplateId());
            template.setTypeIds(typeIds.toArray(new Long[0]));
        }
        return templateList;
    }

    /**
     * u901au8fc7u6a21u677fIDu67e5u8be2u5408u540cu6a21u677f
     *
     * @param templateId u6a21u677fID
     * @return u5408u540cu6a21u677fu4fe1u606f
     */
    @Override
    public ContractTemplate selectContractTemplateByTemplateId(Long templateId)
    {
        ContractTemplate template = contractTemplateMapper.selectContractTemplateByTemplateId(templateId);
        if (template != null)
        {
            // u67e5u8be2u5173u8054u7684u7c7bu578bID
            List<Long> typeIds = contractTemplateTypeMapper.selectTypeIdsByTemplateId(templateId);
            template.setTypeIds(typeIds.toArray(new Long[0]));
        }
        return template;
    }

    /**
     * u6839u636eu6a21u677fu540du79f0u67e5u8be2u5408u540cu6a21u677f
     *
     * @param templateName u6a21u677fu540du79f0
     * @return u5408u540cu6a21u677fu4fe1u606f
     */
    @Override
    public ContractTemplate selectContractTemplateByName(String templateName)
    {
        return contractTemplateMapper.selectContractTemplateByName(templateName);
    }

    /**
     * u68c0u67e5u5408u540cu6a21u677fu540du79f0u662fu5426u552fu4e00
     *
     * @param contractTemplate u5408u540cu6a21u677fu4fe1u606f
     * @return u7ed3u679cu7801 true u552fu4e00 false u4e0du552fu4e00
     */
    @Override
    public boolean checkContractTemplateNameUnique(ContractTemplate contractTemplate)
    {
        Long templateId = StringUtils.isNull(contractTemplate.getTemplateId()) ? -1L : contractTemplate.getTemplateId();
        ContractTemplate info = contractTemplateMapper.selectContractTemplateByName(contractTemplate.getTemplateName());
        if (StringUtils.isNotNull(info) && info.getTemplateId().longValue() != templateId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * u65b0u589eu5408u540cu6a21u677f
     *
     * @param contractTemplate u5408u540cu6a21u677fu4fe1u606f
     * @return u7ed3u679c
     */
    @Override
    @Transactional
    public int insertContractTemplate(ContractTemplate contractTemplate)
    {
        int rows = contractTemplateMapper.insertContractTemplate(contractTemplate);
        insertContractTemplateType(contractTemplate);
        return rows;
    }

    /**
     * u4feeu6539u5408u540cu6a21u677f
     *
     * @param contractTemplate u5408u540cu6a21u677fu4fe1u606f
     * @return u7ed3u679c
     */
    @Override
    @Transactional
    public int updateContractTemplate(ContractTemplate contractTemplate)
    {
        // u5220u9664u539fu6709u5173u8054u5173u7cfb
        contractTemplateTypeMapper.deleteTemplateTypeByTemplateId(contractTemplate.getTemplateId());
        // u65b0u589eu5173u8054u5173u7cfb
        insertContractTemplateType(contractTemplate);
        return contractTemplateMapper.updateContractTemplate(contractTemplate);
    }

    /**
     * u6279u91cfu65b0u589eu5408u540cu6a21u677fu4e0eu5408u540cu7c7bu578bu5173u8054
     *
     * @param contractTemplate u5408u540cu6a21u677fu4fe1u606f
     */
    public void insertContractTemplateType(ContractTemplate contractTemplate)
    {
        Long[] typeIds = contractTemplate.getTypeIds();
        if (StringUtils.isNotNull(typeIds))
        {
            // u65b0u589eu7528u6237u4e0eu89d2u8272u7ba1u7406u8868u6570u636e
            List<ContractTemplateType> list = new ArrayList<ContractTemplateType>();
            for (Long typeId : typeIds)
            {
                ContractTemplateType tt = new ContractTemplateType();
                tt.setTemplateId(contractTemplate.getTemplateId());
                tt.setTypeId(typeId);
                list.add(tt);
            }
            if (list.size() > 0)
            {
                contractTemplateTypeMapper.batchInsertTemplateType(list);
            }
        }
    }

    /**
     * u5220u9664u5408u540cu6a21u677f
     *
     * @param templateId u6a21u677fID
     * @return u7ed3u679c
     */
    @Override
    @Transactional
    public int deleteContractTemplateByTemplateId(Long templateId)
    {
        // u5220u9664u5173u8054u5173u7cfb
        contractTemplateTypeMapper.deleteTemplateTypeByTemplateId(templateId);
        return contractTemplateMapper.deleteContractTemplateByTemplateId(templateId);
    }

    /**
     * u6279u91cfu5220u9664u5408u540cu6a21u677f
     *
     * @param templateIds u9700u8981u5220u9664u7684u6570u636eu4e3bu952eu96c6u5408
     * @return u7ed3u679c
     */
    @Override
    @Transactional
    public int deleteContractTemplateByTemplateIds(Long[] templateIds)
    {
        // u5220u9664u5173u8054u5173u7cfb
        contractTemplateTypeMapper.deleteTemplateTypeByTemplateIds(templateIds);
        return contractTemplateMapper.deleteContractTemplateByTemplateIds(templateIds);
    }

    /**
     * u6279u91cfu5bfcu5165u5408u540cu6a21u677f
     *
     * @param templateList u5408u540cu6a21u677fu5217u8868
     * @param createBy u521bu5efa
     * @return u6210u529fu5bfcu5165u7684u6570u91cf
     */
    @Override
    @Transactional
    public int batchImportContractTemplates(List<ContractTemplate> templateList, String createBy)
    {
        if (StringUtils.isEmpty(templateList))
        {
            return 0;
        }

        int successCount = 0;
        for (ContractTemplate template : templateList)
        {
            // u8bbeu7f6eu521bu5efa
            template.setCreateBy(createBy);

            // u8bbeu7f6eu7c7bu72c0u72b6u6001
            if (StringUtils.isEmpty(template.getStatus()))
            {
                template.setStatus("0");
            }

            // u8bbeu7f6eu6587u4ef6u4fe1u606f
            if (StringUtils.isNotEmpty(template.getFileUrl()))
            {
                // u4e8cu4ef6OSSu8defu5f84u4e4bu4f5cu540du79f0
                String ossPath = template.getFileUrl();
                // 如果路径不是以http或https开头，则添加OSS前缀
                if (!ossPath.startsWith("http://") && !ossPath.startsWith("https://")) {
                    ossPath = "https://contract-review.oss-cn-guangzhou.aliyuncs.com/contracts" + (ossPath.startsWith("/") ? "" : "/") + ossPath +".docx";
                    template.setFileUrl(ossPath);
                }
                String fileName = template.getTemplateName() + ".docx";

                // u8bbeu7f6eu6587u4ef6u76f8u540cu4fe1u606f
                template.setFileName(fileName);
                template.setFileUrl(ossPath);

                // u8bbeu7f6eu6587u4ef6u7c7bu578b
                if (fileName.contains("."))
                {
                    String fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
                    template.setFileType(fileType);
                }

                // u8bbeu7f6eu6587u4ef6ID
                template.setFileId(String.valueOf(System.currentTimeMillis()));
            }

            // u8bbeu7f6eu63cfu8ff0u7b80
            if (StringUtils.isEmpty(template.getDescription()))
            {
                template.setDescription(template.getTemplateName());
            }

            // u8bbeu7f6eu5907u6ce8
            if (StringUtils.isEmpty(template.getRemark()))
            {
                template.setRemark(template.getTemplateName());
            }

            // u68c0u67e5u6a21u677fu540du79f0u662fu5426u552fu4e00
            if (checkContractTemplateNameUnique(template))
            {
                // u65b0u589eu6a21u677f
                int rows = contractTemplateMapper.insertContractTemplate(template);
                if (rows > 0)
                {
                    // u65b0u589eu5173u8054u5173u7cfb
                    insertContractTemplateType(template);
                    successCount++;
                }
            }
        }

        return successCount;
    }
}
